//
//  TaskListViewController.m
//  Trackit
//
//  Created by yaodaobin on 2017/4/4.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "TaskListViewController.h"
#import "TaskListModel.h"
#import "TaskListCell.h"
#import "TaskDetailViewController.h"

@interface TaskListViewController ()

@end

@implementation TaskListViewController

#pragma mark - Controller
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initParams];
    [self initUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestTaskList];
}

#pragma mark - Init
- (void)initParams {
    [super initParams];
    self.hideRefreshHeader = YES;
    self.hideRefreshFooter = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestTaskList) name:kTaskFinishNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestTaskList) name:kTaskSaveLocalNotification object:nil];
}

- (void)initUI {
    [super initUI];
    UIBarButtonItem *leftBtn = [UIBarButtonItem barItemWithTitle:@"" target:self action:@selector(logout)];
    self.navigationItem.leftBarButtonItem = leftBtn;

    UIBarButtonItem *rightBtn = [UIBarButtonItem barItemWithTitle:NSLocalizedString(@"退出登录", nil) target:self action:@selector(logout)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    self.tableView.estimatedRowHeight = 60;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView registerClass:[TaskListCell class] forCellReuseIdentifier:NSStringFromClass([TaskListCell class])];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
}

#pragma mark - Override
- (void)pullReload {
    [self requestTaskList];
}

- (void)loadMore {
    
}

#pragma mark - Request
- (void)requestTaskList {
    User *user = APP_DELEGATE.user;
    if (user) {
        kWeakSelf
        [self showWaitingIcon];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic safeSetObject:user.userName forKey:@"username"];
        [dic safeSetObject:user.password forKey:@"password"];

        [TrackitDataEngine getTaskList:dic onComplete:^(NSDictionary *data, NSError *error) {
            [weakSelf dismissWaitingIcon];
            if (data) {
                NSMutableArray *array = [NSMutableArray array];
                for (NSDictionary *dic in data[@"Data"]) {
                    TaskListModel *model = [[TaskListModel alloc] initWithData:dic];
                    [array addObject:model];
                }
                [weakSelf.data removeAllObjects];
                [weakSelf.data addObjectsFromArray:array];
                [weakSelf.tableView reloadData];
            } else {
                [TKUtil showMessage:error.userInfo[@"message"]];
            }
        }];
    }
}

#pragma mark - Private
- (void)logout {
    APP_DELEGATE.user = nil;
    [self back];
}

- (void)reloadTable {
    [self.tableView reloadData];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TaskListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TaskListCell class])];
    TaskListModel *model = self.data[indexPath.row];
    [cell setTaskListModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TaskListModel *model = self.data[indexPath.row];
    TaskDetailViewController *vc = [[TaskDetailViewController alloc] init];
    vc.taskDetailID = model.taskDetailID;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
